package com.egao.common.manage.school;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.company.constants.MutualConstants;
import com.egao.common.module.company.entity.MutualApply;
import com.egao.common.module.company.entity.vo.*;
import com.egao.common.module.company.service.MutualApplyService;
import com.egao.common.module.company.service.MutualService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 学校双选会管理
 * @author dl
 */
@Api(tags = "学校双选会管理")
@RestController
@RequestMapping("${api}")
public class SchoolMutualController extends BaseController {

    @Autowired
    private MutualService mutualService;

    @Autowired
    private MutualApplyService mutualApplyService;

    @PreAuthorize("hasAuthority('school:mutual:list')")
    @ApiOperation(value = "双选会信息", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "双选会信息", desc = "分页查询双选会信息")
    @ApiPageParam
    @GetMapping("/school/mutual/page")
    public PageResult<MutualListVO> page(HttpServletRequest request) {
        PageParam<MutualListVO> pageParam = new PageParam<>(request);
        return new PageResult<>(mutualService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:mutual:save')")
    @OperLog(value = "添加双选会信息", desc = "添加双选会信息", result = true)
    @ApiOperation(value = "添加双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualFormVO", value = "mutualFormVO", required = true, dataType = "MutualFormVO")
    })
    @PostMapping("/school/mutual/save")
    public JsonResult save(@Valid @RequestBody MutualFormVO mutualFormVO) {
        mutualFormVO.setCreateBy(getLoginUserId());
        return mutualService.saveMutual(mutualFormVO);
    }

    @PreAuthorize("hasAuthority('school:mutual:update')")
    @OperLog(value = "修改双选会信息", desc = "修改双选会信息", result = true)
    @ApiOperation(value = "修改双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualFormVO", value = "mutualFormVO", required = true, dataType = "MutualFormVO")
    })
    @PutMapping("/school/mutual/update")
    public JsonResult update(@Valid @RequestBody MutualFormVO mutualFormVO) {
        return mutualService.updateMutual(mutualFormVO);
    }

    @PreAuthorize("hasAuthority('school:mutual:check')")
    @OperLog(value = "审核双选会报名信息", desc = "审核双选会报名信息", result = true)
    @ApiOperation(value = "审核双选会报名信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualApplyFormVO", value = "mutualApplyFormVO", required = true, dataType = "MutualApplyFormVO")
    })
    @PutMapping("/school/mutual/check")
    public JsonResult check(@Valid @RequestBody MutualApplyFormVO mutualApplyFormVO) {
        mutualApplyFormVO.setCheckUserId(getLoginUserId());
        return mutualService.checkMutual(mutualApplyFormVO);
    }

    @PreAuthorize("hasAuthority('school:mutual:delete')")
    @OperLog(value = "删除双选会信息", desc = "删除双选会信息", result = true)
    @ApiOperation(value = "删除双选会信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualFormVO", value = "mutualFormVO", required = true, dataType = "MutualFormVO")
    })
    @PutMapping("/school/mutual/delete")
    public JsonResult deleteMutual(@RequestBody MutualFormVO mutualFormVO) {
        return mutualService.deleteMutual(mutualFormVO);
    }

    @PreAuthorize("hasAuthority('school:mutual:delete')")
    @OperLog(value = "删除双选会报名信息", desc = "删除双选会报名信息", result = true)
    @ApiOperation(value = "删除双选会报名信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mutualApplyFormVO", value = "mutualApplyFormVO", required = true, dataType = "MutualApplyFormVO")
    })
    @PutMapping("/school/mutual/delete-apply")
    public JsonResult deleteMutualApply(@RequestBody MutualApplyFormVO mutualApplyFormVO) {
        return mutualApplyService.deleteMutualApply(mutualApplyFormVO);
    }

    @PreAuthorize("hasAuthority('school:mutual:detail')")
    @ApiOperation(value = "获取双选会信息详情")
    @OperLog(value = "获取双选会信息详情", desc = "获取双选会信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "双选会信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/mutual/detail")
    public JsonResult detail(Integer id) {
        return mutualService.detail(id);
    }

    @PreAuthorize("hasAuthority('school:mutual:list')")
    @ApiOperation(value = "查询报名列表", notes = "查询报名列表")
    @OperLog(value = "查询报名列表", desc = "查询报名列表")
    @ApiPageParam
    @GetMapping("/school/mutual/query")
    public PageResult<MutualApplyListVO> query(HttpServletRequest request) {
        PageParam<MutualListVO> pageParam = new PageParam<>(request);
        pageParam.put("invalid", MutualConstants.INVALID_NO);
        return new PageResult<>(mutualService.queryApply(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:mutual:list')")
    @ApiOperation(value = "查询取消报名列表", notes = "查询取消报名列表")
    @OperLog(value = "查询取消报名列表", desc = "查询取消报名列表")
    @ApiPageParam
    @GetMapping("/school/mutual/query-cancel")
    public JsonResult queryCancel(HttpServletRequest request) {
        PageParam<MutualListVO> pageParam = new PageParam<>(request);
        pageParam.put("invalid", MutualConstants.INVALID_YES);
        return mutualService.queryCancelApply(pageParam);
    }

    @PreAuthorize("hasAuthority('school:mutual:detail')")
    @ApiOperation(value = "获取双选会信息详情")
    @OperLog(value = "获取双选会信息详情", desc = "获取双选会信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "双选会信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/mutual/detail-apply")
    public JsonResult detailApply(Integer id) {
        return mutualApplyService.detail(id);
    }

    @PreAuthorize("hasAuthority('school:mutual:list')")
    @ApiOperation(value = "重置单位取消")
    @OperLog(value = "重置单位取消", desc = "将单位取消改为不取消且待审核", result = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "双选会信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/mutual/reset-cancel")
    public JsonResult resetCancel(Integer id) {
        return mutualApplyService.resetCancel(id, getLoginUserId());
    }
}
